package com.hk.service.impl;

import java.math.BigDecimal;

import javax.annotation.Resource;

import com.hk.commons.entity.Organization;
import com.hk.service.IOrganization_New;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.search.jmx.StatisticsInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.hk.commons.ConstantStatistics;
import com.hk.commons.ConstantUserChannel;
import com.hk.commons.vo.StatisticsMsg;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.order.IStatisticsInfo;

@Service
public class StatisticsInfoImpl extends BaseServiceImpl<StatisticsInfo>
		implements IStatisticsInfo {
	
	private Logger logger = LoggerFactory.getLogger(StatisticsInfoImpl.class);

	@Autowired
	private IOrganization_New orgNewService;

	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 部门理财师人数
	 */
	@Override
	public Integer getFinancialPlannerNumByOrgCode(String orgCode,
			Long begTime, Long endTime) {

		Integer size = 0;
		String sql = null;
		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = "SELECT count(1) FROM userview  where isInside = 1";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			size = 0;
		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			//直销二部
			sql = "SELECT count(*) FROM userview where isInside = 1 and userChannel = " + ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02 ;
		} else {
			sql = "SELECT count(*) FROM userview where  isInside = 1 and userOrgCode  like '" + orgCode + "-%'";
		}

		if (StringHelper.isNotEmpty(sql)) {
			if (begTime != null) {
				sql += " and registCts > " + begTime;
			}

			if (endTime != null) {
				sql += " and registCts < " + endTime;
			}

			size = jdbcTemplate.queryForObject(sql, Integer.class);
		}

		//logger.info("getFinancialPlannerNumByOrgCode sql = " + sql);
		return size;
	}

	/**
	 * 部门绑卡总人数, 不包括理财师
	 */
	@Override
	public Integer getTiedCardUserAccountByOrgCode(String orgCode,
			Long begTime, Long endTime) {
		String sql = "";

		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = "SELECT count(1) FROM userview as a where tieCard = 1  and isInside != 1  ";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			//直销二部
			sql = "SELECT count(1) FROM userview as s where tieCard = 1 and isInside != 1 and userChannel ="+ConstantUserChannel.USER_CHANNEL_WUZHU ;

		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			sql = "SELECT count(1) FROM userview  where isInside != 1 and tieCard = 1 and  userChannel = " + ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02 ;
		} else {
			sql = "SELECT count(1) FROM userview where isInside != 1 and tieCard = 1  and finOrgCode  like '" + orgCode + "-%'";
		}

		if (begTime != null) {
			sql += " and registCts > " + begTime;
		}

		if (endTime != null) {
			sql += " and registCts < " + endTime;
		}

		// logger.info("getTiedCardUserAccountByOrgCode sql = " + sql);

		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

		return size == null ? 0 : size;

	}

	/**
	 * 部门客户总数
	 */
	@Override
	public Integer getRelationUserAccountByOrgCode(String orgCode,
			Long begTime, Long endTime) {

		String sql = "";
		// 无主客户服务部
		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = "SELECT count(1) FROM userview where isInside != 1 ";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			sql = "SELECT count(1) FROM userview where isInside != 1 and userChannel ="+ConstantUserChannel.USER_CHANNEL_WUZHU ;
		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			
			// 直销二部
			sql = "SELECT count(1) FROM userview where isInside != 1 and userChannel = "+ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02;
		} else {
			sql = "SELECT count(1) FROM userview where isInside != 1  and finOrgCode  like '" + orgCode + "-%'";
		}

		if (begTime != null) {
			sql += " and registCts > " + begTime;
		}

		if (endTime != null) {
			sql += " and  registCts < " + endTime;
		}

		//logger.info("getRelationUserAccountByOrgCode sql = " + sql);

		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

		return size == null ? 0 : size;
	}

	/**
	 * 部门预约订单总数
	 */
	@Override
	public Integer getBespeakOrderNumByOrgCode(String orgCode, Long begTime,
			Long endTime, Long parId) {

		String sql = "";
		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = " SELECT count(1) FROM reservView01 where 1 = 1";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			sql = "SELECT count(1) FROM reservView01 where channel = " + ConstantUserChannel.USER_CHANNEL_WUZHU ;
		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			sql = "SELECT count(1) FROM reservView01 where channel = "+ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02;
		} else {
			sql = "SELECT count(1) FROM reservView01 where reservOrgCode like '" + orgCode + "-%'";
		}
		
		
		if (begTime != null) {
			sql += " and  cts > " + begTime;
		}

		if (endTime != null) {
			sql += " and  cts < " + endTime;
		}

		if (parId != null) {
			sql += " and productId in ( select id from t_product_info where parId = " + parId +") "  ;
		}

		//logger.info("部门预约订单总数 : getBespeakOrderNumByOrgCode sql = " + sql);
		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);
		return size == null ? 0 : size;
	}

	/**
	 * 部门预约总金额
	 */
	@Override
	public BigDecimal getBespeakOrderAmountByOrgCode(String orgCode,
			Long begTime, Long endTime, Long parId) {

		String sql = "";
		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = " SELECT sum(amount) FROM reservView01 where 1 = 1";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			sql = "SELECT sum(amount) FROM reservView01 where channel = " + ConstantUserChannel.USER_CHANNEL_WUZHU ;
		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			sql = "SELECT sum(amount) FROM reservView01 where channel = "+ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02;
		} else {
			sql = "SELECT sum(amount) FROM reservView01 where reservOrgCode like '" + orgCode + "-%'";
		}

		if (begTime != null) {
			sql += " and  cts > " + begTime;
		}

		if (endTime != null) {
			sql += " and cts < " + endTime;
		}

		if (parId != null) {
			sql += " and productId in ( select id from t_product_info where parId = " + parId +") "  ;
		}

		BigDecimal totalAmount = jdbcTemplate.queryForObject(sql,
				BigDecimal.class);
		if (totalAmount == null) {
			totalAmount = new BigDecimal(0);
		}
		
		//logger.info("部门预约总金额 : getBespeakOrderAmountByOrgCode sql = " + sql);
		return totalAmount;

	}

	/**
	 * 部门付款订单总数
	 */
	@Override
	public Integer getPayOrderNumByOrgCode(String orgCode, Long begTime,
			Long endTime, Long parId) {

		String sql = "";
		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = "SELECT count(*) FROM niggerView where 1 = 1";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			sql = "SELECT count(*) FROM niggerView where orderChannel ="+ConstantUserChannel.USER_CHANNEL_WUZHU; 
		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			sql = "SELECT count(*) FROM niggerView where orderChannel ="+ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02; 
		} else {
			sql = "SELECT count(*) FROM niggerView where orderOrgCode like '"+ orgCode + "-%'";
		}

		if (begTime != null) {
			sql += " and orderCreatedOn > " + begTime;
		}

		if (endTime != null) {
			sql += " and orderCreatedOn < " + endTime;
		}

		if (parId != null) {
			sql += " and productId in ( select id from t_product_info where parId = " + parId +") "  ;
		}
		//logger.info("部门付款订单总数: getPayOrderNumByOrgCode sql = " + sql);
		
		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);
		return size == null ? 0 : size;

	}

	/**
	 * 部门付款订单金额
	 */
	@Override
	public BigDecimal getPayOrderAmountByOrgCode(String orgCode, Long begTime,
			Long endTime, Long parId) {

		String sql = "";
		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = "SELECT sum(successAmount) FROM niggerView where 1 = 1 ";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			sql = "SELECT sum(successAmount) FROM niggerView  where orderChannel ="+ConstantUserChannel.USER_CHANNEL_WUZHU; 
		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			sql = "SELECT sum(successAmount) FROM niggerView where orderChannel ="+ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02; 					
		} else {
			sql = "SELECT sum(successAmount) FROM niggerView where orderOrgCode like '" + orgCode + "-%'";
		}
		if (begTime != null) {
			sql += " and orderCreatedOn > " + begTime;
		}

		if (endTime != null) {
			sql += " and orderCreatedOn < " + endTime;
		}

		if (parId != null) {
			sql += " and productId in ( select id from t_product_info where parId = " + parId +") "  ;
		}

		BigDecimal totalAmount = jdbcTemplate.queryForObject(sql,
				BigDecimal.class);

		if (totalAmount == null) {
			totalAmount = new BigDecimal(0);
		}
		
		//logger.info("部门付款订单总金额: getPayOrderAmountByOrgCode sql = " + sql);
		
		return totalAmount;
	}

	/**
	 * 部门付款订单金额
	 */
	@Override
	public BigDecimal getPayNiggerAmountByOrgCode(String orgCode, Long begTime,/*订单需要更改！*/
			Long endTime, Long parId) {

		String sql = "";
		if (orgCode.equals(ConstantStatistics.HKCF_TOTAL)) {
			sql = "SELECT sum(niggerAmount) FROM niggerView where 1 = 1 ";
		} else if (orgCode.equals(ConstantStatistics.EMPTY_RECOMMEND_ORG_CODE)) {
			sql = "SELECT sum(niggerAmount) FROM niggerView where orderChannel ="+ConstantUserChannel.USER_CHANNEL_WUZHU; 
		} else if (orgCode
				.equals(ConstantStatistics.ZHIXIAOERBU_RECOMMEND_ORG_CODE)) {
			//直销二部
			sql = "SELECT sum(niggerAmount) FROM niggerView where orderChannel ="+ConstantUserChannel.USER_CHANNEL_ZHIXIAO_02; 
		} else {
			sql = "SELECT sum(niggerAmount) FROM niggerView where orderOrgCode like '" + orgCode + "-%'";
		}
		if (begTime != null) {
			sql += " and orderCreatedOn > " + begTime;
		}

		if (endTime != null) {
			sql += " and orderCreatedOn < " + endTime;
		}

		if (parId != null) {
			sql += " and productId in ( select id from t_product_info where parId = " + parId +") "  ;
		}
		BigDecimal totalAmount = jdbcTemplate.queryForObject(sql,
				BigDecimal.class);

		if (totalAmount == null) {
			totalAmount = new BigDecimal(0);
		}

		//logger.info("getPayOrderAmountByOrgCode sql = " + sql);

		return totalAmount;
	}






	@Override
	public StatisticsMsg getInfo(StatisticsMsg statisInfo) {

		String code = statisInfo.getCode();
		Long begTime = statisInfo.getStartTime();
		Long endTime = statisInfo.getEndTime();
		Long parId = statisInfo.getParId();

		// 该部门总人数
		statisInfo.setFinancialPlannerNum(getFinancialPlannerNumByOrgCode(code,
				begTime, endTime));

		// 总推荐人数
		statisInfo.setRelationUserAccount(getRelationUserAccountByOrgCode(code,
				begTime, endTime));

		// 绑卡用户总数
		statisInfo.setTiedCardUserAccount(getTiedCardUserAccountByOrgCode(code,
				begTime, endTime));

		// 预约订单总数
		statisInfo.setBespeakOrderNum(getBespeakOrderNumByOrgCode(code,
				begTime, endTime, parId));

		// 预约订单总数
		statisInfo.setBespeakOrderAmount(getBespeakOrderAmountByOrgCode(code,
				begTime, endTime, parId));

		// 付款订单总数
		statisInfo.setPayOrderNum(getPayOrderNumByOrgCode(code, begTime,
				endTime, parId));

		// 付款订单总金额
		statisInfo.setPayOrderAmount(getPayOrderAmountByOrgCode(code, begTime,
				endTime, parId));
		statisInfo.setNiggerAmmount(getPayNiggerAmountByOrgCode(code, begTime,
				endTime, parId));
		return statisInfo;
	}
}
